/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.repository;

import me.zhengjie.modules.system.domain.SalePoorProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-02-29
**/
public interface SalePoorProductRepository extends JpaRepository<SalePoorProduct, Long>, JpaSpecificationExecutor<SalePoorProduct> {

    @Query(value = "select min(id) as id, d.model, ?1 as from_now_day, ?2 as greater_from_now_day, count(*) as purchase_time, sum(quantity) as quantity " +
                   "from purchase_data d where customer = '备' and DATEDIFF(NOW(), d.create_time) < ?1 and DATEDIFF(NOW(), d.create_time) > ?2 " +
                   "group by d.model having count(*) <= ?3 ",
            countQuery = "select count(*) from (select min(id) as id, d.model, ?1 as from_now_day, ?2 as greater_from_now_day, count(*) as purchase_time, sum(quantity) as quantity " +
                    "from purchase_data d where customer = '备' and DATEDIFF(NOW(), d.create_time) < ?1 and DATEDIFF(NOW(), d.create_time) > ?2 " +
                    "group by d.model having count(*) <= ?3) v",
            nativeQuery = true )
    Page<SalePoorProduct> querySalePoorProduct(@Param("fromNowDay") Integer fromNowDay,
                                               @Param("greaterFromNowDay") Integer greaterFromNowDay,
                                               @Param("purchaseTime") Integer purchaseTime, Pageable pageable);
    @Query(value = "select min(id) as id, d.model, ?1 as from_now_day, ?2 as greater_from_now_day, count(*) as purchase_time, sum(quantity) as quantity " +
            "from purchase_data d where customer = '备' and DATEDIFF(NOW(), d.create_time) < ?1 and DATEDIFF(NOW(), d.create_time) > ?2 " +
            "group by d.model having count(*) <= ?3 ",
            nativeQuery = true )
    List<SalePoorProduct> querySalePoorProduct(@Param("fromNowDay") Integer fromNowDay,
                                               @Param("greaterFromNowDay") Integer greaterFromNowDay,
                                               @Param("purchaseTime") Integer purchaseTime);

}